home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13629 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Class Engineering Issues
  5. Date: Tue, 26 Mar 1996 12:09:31 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <315824CB.3F94@datalytics.com>
  8. References: <3156ecf9.611110371@sun.cis.smu.edu>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Damon Bowman wrote:
  16. > I am learning C++, and I think I need some additional rules of thumb
  17. > to use when constructing classes.
  18. > What are some general rules of thumb to use when deciding whether or
  19. > not a particular function should be a class member or not?
  20. > Simply put, there aren't any.  The concept of a class dicates 
  21. what should be in it.  If you have a class describing a certain 
  22. thing, then all information and functions for manipulating that 
  23. information should be part of that class.  (There are exceptions 
  24. required for things like commutivity.)  Once you've identified 
  25. the classes required to describe the problem, you need to 
  26. determine what they must do and know.
  27.  
  28. I have seen people create a class for the sole purpose of 
  29. collecting functions in one place.  They apparently think that 
  30. C++ requires everything to be in a class.  If the function 
  31. doesn't apply to a class, don't force it.  If it applies to more 
  32. than one class, perhaps it should be a base class member 
  33. function.
  34.  
  35. > What are some rules for deciding if a function should be public or
  36. > private?
  37. > You must determine "who" needs access to it.  If it provides 
  38. only implementation that no one, not even derived classes, 
  39. should use, it should be private.  If it is fundamental to the 
  40. use of the class it should, possibly, be public.  The 
  41. distinction between protected and public access is gray.  You 
  42. have to decide whether the function is one you want to make 
  43. available always to users of your class or those derived from 
  44. it.  If you think it should be restricted--perhaps due to the 
  45. damage it can do--you might make it protected and require a 
  46. derived class to expose it should that become necessary.  
  47. Other than that, you're restricting use to classes derived from 
  48. yours, saying, in effect, I'm restricting access purposely 
  49. because I don't think the general class user should invoke this 
  50. function.
  51.  
  52. > Should argument checking ALWAYS be done by the member functions?  What
  53. > criteria are used to decide this?
  54.  
  55. I frequently code debug version argument checking using 
  56. assertions.  For things that can be devastating, like a null 
  57. pointer dereference, I code a runtime validation.  The former 
  58. helps the developer (even you) use the function correctly before 
  59. the code goes production.  The latter generates robust code.
  60.  
  61. Did I answer your questions sufficiently?
  62.  
  63. -- 
  64. Robert Stewart        | My opinions are usually my own.
  65. Datalytics, Inc.    | stew@datalytics.com
  66.